<?php

class UsersAction extends CommonAction {

    public function index() {

        $this->assign("admin", M('Users')->find(1));
        $this->assign("users", M('Users')->where("id > 1")->select());

        $this->display();
    }

    public function modifyPwdPage() {
        $this->display();
    }

    public function addUserPage() {
        $this->display();
    }

    public function savePwd() {


        $id = I('post.id', 0);
        $oldPassword = I('post.oldPassword', '');
        $newPassword = I('post.newPassword', '');

        $res = D("Users")->savePwd($id, $oldPassword, $newPassword);

        switch ($res) {
            case -1:
                $this->ajaxReturn(array('status' => -1, 'info' => '旧密码错误', 'data' => null, 'jumpUrl' => null));
                break;
            case 0:
                $this->ajaxReturn(array('status' => 0, 'info' => '修改失败', 'data' => null, 'jumpUrl' => null));
                break;
            case 1:
                $this->ajaxReturn(array('status' => 1, 'info' => '修改成功', 'data' => null, 'jumpUrl' => U('Main/index')));
                break;
        }
    }

    public function addUser() {

        $db_user = M('Users');
        if ($db_user->create()) {
            $db_user->create_time = date("Y-m-d H:i:s",time());
            $res = $db_user->add();
            if ($res) {
                $this->ajaxReturn(array('status' => 1, 'info' => '新增成功！', 'data' => null, 'jumpUrl' => U('Users/index')));
            } else {
                $this->ajaxReturn(array('status' => 0, 'info' => '新增失败', 'data' => null, 'jumpUrl' => null));
            }
        }
    }

    public function delUser() {
        # code...
        $id = I("id", -1);
        $res = M('Users')->delete($id);
        if ($res) {
            $this->ajaxReturn(array('status' => 1, 'info' => '删除成功！', 'data' => null, 'jumpUrl' => U('Users/index')));
        } else {
            $this->ajaxReturn(array('status' => 0, 'info' => '删除失败', 'data' => null, 'jumpUrl' => null));
        }
    }

}

?>